﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Data;
using ReflectionStudio.Core.Events;
using System.Windows.Controls;
using System.Collections;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class TWZ09
    {
        public TWZ09()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBBST();
            this.DataContext = MV;
            MESSAGES += "WBREQ;WBUNREQ;";
            TargetMTR = 100000;
            InitializeComponent();
            IsCanDelete = false;
        }
        ITXEXRPT ITXEXRPT;


        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
            if (IsFreeWB)
            {
                cvMaster.CustomFilter = "WBBAUFLFD='000000'";
            }
        }
        bool IsWB61 = false;
        bool IsWB62 = false;
        public override void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            base.bgWorker_DoWork(sender, e);
            EventDispatcher.Instance.RaiseStatus("开始留位", StatusEventType.StopProgress);
            if (IsWB61)
            {
                TMV.AUTOWB61(TMV.IsFreeWB);
            }
            if (IsWB62)
            {
                TMV.WBFREE(true);
            }
        }
        public override void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            base.bgWorker_RunWorkerCompleted(sender, e);
            string key = ORDERNUM;
            WB61VVT = "";
            WB61VVT = key;
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwWB = dwMaster;
            if (dwWB == null)
            {
                TAX = "";
                TXTISIND = "";
            }
            else
            {
                TAX = dwWB["WBBTAX"].ToString();
                TXTISIND = dwWB["WBBRWKRS"].ToString() + dwWB["WBBRWLFD"].ToString() + dwWB["WBBPOS"].ToString().PadRight(52);

            }
        }

        public override void OnWorkflowIDChange(MessageEventArgs e)
        {
            MessageInfo msg;
            base.OnWorkflowIDChange(e);
            msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {
                case "WBREQ":
                    AUFTYP = msg.AUFTYP;
                    KRS = msg.KRS;
                    LFD = msg.LFD;
                    POS = msg.POS;
                    LIEFSCH = msg.LIEFSCH;
                    msg.LO = LO;
                    msg.LB = LB;
                    WBREQ(msg);
                    IsVVLIEF = false;//是否需要显示加入发货单的提示信息
                    break;
                case "WBUNREQ":
                    IsVVLIEF = true;
                    TBMain.Clear();
                    break;
            }
        }

        public void WBREQ(MessageInfo msg)
        {
            TBMain.Clear();
            TMV.WBREQ(msg);
            TBMain.AcceptChanges();
        }

        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            if (IsEdit)
            {
                MessageBox.Show("已处于编辑状态，请先将数据保存后才可再次进行编辑！");
                return;
            }

            MV.TBTemp = null;
            MV.TBTemp = TBMain.Copy();
            base.EditCommand(sender, e);
            IsEdit = true;
        }

        public override void NewCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {

                IsWB62 = false;
                IsWB61 = true;
                WBROWS = new List<DataRow>();
                DataTable TB = TBMain.Clone();
                foreach (var item in dlgMaster.SelectedItems)
                {
                    if (!item.GetType().ToString().Contains("DataRowView")) continue;

                    DataRow _dwWB = ((System.Data.DataRowView)item).Row;

                    if (_dwWB["WBBAUFLFD"].ToString() != "000000")
                    {
                        continue;
                    }
                    dwData = TB.Rows.Add();
                    dwData.ItemArray = _dwWB.ItemArray;
                    dwData["WBBAUFTYP"] = AUFTYP;
                    dwData["WBBAUFKRS"] = KRS;
                    dwData["WBBAUFLFD"] = LFD;
                    dwData["WBBAUFPOS"] = POS;
                    dwData["WBBTP"] = "0001";
                    dwData["WBBAENDBEN"] = app.User;
                    dwData["WBBAENDDAT"] = MV.date;
                }
                foreach (DataRow _dwWB in TB.Rows)
                {
                    WBROWS.Add(_dwWB);
                    filter = "WBBSTKNR='" + _dwWB["WBBSTKNR"].ToString() + "'";
                    rows = TBMain.Select(filter);
                    foreach (DataRow row in rows)
                    {
                        row.ItemArray = _dwWB.ItemArray;
                    }
                }
                bgWorker.RunWorkerAsync();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void DeleteCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            IsWB62 = true;
            IsWB61 = false;
            WBROWS = new List<DataRow>();
            DataTable TB = TBMain.Clone();
            foreach (var item in dlgMaster.SelectedItems)
            {
                if (!item.GetType().ToString().Contains("DataRowView")) continue;

                DataRow _dwWB = ((System.Data.DataRowView)item).Row;

                if (_dwWB["WBBAUFLFD"].ToString() == "000000")
                {
                    continue;
                }
                dwData = TB.Rows.Add();
                dwData.ItemArray = _dwWB.ItemArray;
            }
            foreach (DataRow _dwWB in TB.Rows)
            {
                WBROWS.Add(_dwWB);
            }
            bgWorker.RunWorkerAsync();
        }


        private void FreeQtyCommand(object sender, RoutedEventArgs e)
        {
            cvMaster.CustomFilter = "WBBAUFLFD='000000'";
        }

        private void LockedQtyCommand(object sender, RoutedEventArgs e)
        {
            cvMaster.CustomFilter = "WBBAUFLFD<>'000000'";
        }

        private void AllQtyCommand(object sender, RoutedEventArgs e)
        {
            cvMaster.CustomFilter = null;
        }

        private void WBCUTCommand(object sender, RoutedEventArgs e)
        {
            if (dwWB == null)
                return;
            if (dwWB["WBBAUFLFD"].ToString() != "000000")
            {
                MessageBox.Show("已经被留位，需要先取消留位");
                return;
            }
            VICC_WFEVENTS.SVWF.WB10(RUNID, dwWB["WBBSTKNR"].ToString());
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            TMV.BATCHWBCHG();
            base.SaveCommand(sender, e);
            IsEdit = false;
        }

        private void WB4XCommand(object sender, RoutedEventArgs e)
        {
            if (dwWB == null)
                return;
            if (dwWB["WBBAUFLFD"].ToString() != "000000")
            {
                MessageBox.Show("已经被留位，需要先取消留位");
                return;
            }
            VICC_WFEVENTS.SVWF.WB10(RUNID, dwWB["WBBSTKNR"].ToString());
        }

        public override void LOADBYAUF()
        {
            try
            { 
                if (ORDERNUM.Length < 20)
                {
                    TBMain.Clear();
                    return;
                }
                PM["ART"] = "";
                dwData = TMV.VVKP.TBMain.NewRow();
                dwData["VKPMANDANT"] = app.Mandant;
                dwData["VKPAUFKRS"] = ORDERNUM.Substring(1, 2);
                dwData["VKPAUFLFD"] = ORDERNUM.Substring(3, 6);
                dwData["VKPAUFPOS"] = ORDERNUM.Substring(9, 3);
                SResult = TMV.VVKP.QVKP18(dwData);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {

                    PM["WAKZ"] = "F";
                    PM["ART"] = row["VAFPART"].ToString();
                    PM["VAR"] = row["VAFPVAR"].ToString();
                    PM["FARBE"] = row["VAFFFARBE"].ToString();
                    PM["BESCHICH"] = row["VAFBESCHICH"].ToString();
                    PM["LO"] = LO;
                    PM["LB"] = LB;
                }
                if (PM["ART"].ToString().Trim() == "")
                {
                    throw new Exception("请核对销售品种的定义");
                }
                AUFTYP = ORDERNUM.Substring(0, 1);
                KRS = ORDERNUM.Substring(1, 2);
                LFD = ORDERNUM.Substring(3, 6);
                POS = ORDERNUM.Substring(9, 3);
                LIEFSCH = ORDERNUM.Substring(12, 8);
                TMV.WBREQ(PM);
                TBMain.AcceptChanges();
                base.LOADBYAUF();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }


        private void PrintLBCommand(object sender, RoutedEventArgs e)
        {
            if (ITXEXRPT == null)
            {
                ITXEXRPT = new WPFExcelReport.ITXEXRPT();
                ITXEXRPT.Root = "C:\\IntexReport";
                ITXEXRPT.strCompany = app.company;
                ITXEXRPT.PrintType = "OpenExcel";
            }
            ITXEXRPT.dsSource = new DataSet();
            bool IsFound = false;
            if (TBMain.Rows.Count == 0)
                return;
            DataTable tb = TBMain.Clone();
            foreach (var item in dlgMaster.SelectedItems)
            {
                if (!item.GetType().ToString().Contains("DataRowView")) continue;

                DataRow dwItem = ((System.Data.DataRowView)item).Row;
                DataRow _dwWB = tb.Rows.Add();
                _dwWB.ItemArray = dwItem.ItemArray;
            }
            switch (app.company)
            {
                default:
                case "JLT":
                    ITXEXRPT.dsSource.Merge(tb);
                    ITXEXRPT.DocType = "JLTWBD10IT";
                    foreach (DataRow row in tb.Rows)
                    {
                        string PACKNR = row["WBBPACKNR"].ToString();
                        if (PACKNR == "0000")
                            continue;
                        string art = row["WBBART"].ToString().Trim();
                        string dr = @"C:\Program Files\mabu\templates";
                        string filename = "JLB" + art.Substring(3, 4) + ".xls";
                        if (!System.IO.Directory.Exists(dr))
                        {
                            IsFound = false;
                        }
                        else
                        {
                            string[] files = System.IO.Directory.GetFiles(dr);
                            foreach (string file in files)
                            {
                                if (!file.Contains(filename)) continue; IsFound = true; ITXEXRPT.strExcelFile = file; break;
                            }
                            if (!IsFound)
                            {
                                filename = "coillabel" + art.Substring(3, 4) + ".xls";
                                foreach (string file in files)
                                {
                                    if (!file.Contains(filename)) continue; IsFound = true; ITXEXRPT.strExcelFile = file; break;
                                }
                            }
                        }
                    }
                    if (IsFound) ITXEXRPT.WriteExcelReport();
                    break;
            }
        }
        /*
        private void cFree_GotFocus(object sender, RoutedEventArgs e)
        {
            
        }

        private void cLock_GotFocus(object sender, RoutedEventArgs e)
        { 
            cvLock.CustomFilter = "WBBAUFLFD<>'000000'"; 
        } */
    }
}
